package net.asfun.jvalog.domain;

import javax.jdo.Query;

import net.asfun.jvalog.po.Map;
import net.asfun.jvalog.po.Web;
import net.asfun.jvalog.service.Jdoer;
import net.asfun.jvalog.service.JdoerFactory;
import net.asfun.jvalog.task.UpdateManager;

public class Blog {
	
	private Web web;
	private Map theme;
	private Map keywords;
	private Map base;
	private Map version;
	
	private Blog(){};
	
	public static Blog info() {
		Blog myblog = new Blog();
		Jdoer<Web> dbw = JdoerFactory.getWebJdo();
		Query query = dbw.prepareQuery();
		query.setFilter("flag < 0");
		myblog.web = dbw.unique(query);
		if ( myblog.web == null ) {
			myblog.web = new Web();
			myblog.web.setDescription("A blog prowed by GAE JAVA");
			myblog.web.setSubtitle("Webcome to jvaLog");
			myblog.web.setTitle("jvaLog");
			myblog.web.setUrl("/");
			myblog.web.setFlag(-1);
			dbw.save(myblog.web);
		}
		Jdoer<Map> dbm = JdoerFactory.getMapJdo();
		myblog.theme = getMap(dbm, "theme", "default"); 
		myblog.base = getMap(dbm, "base", "http://jvalog.appspot.com");
		myblog.keywords = getMap(dbm, "keywords", "java,appengine,gae,jdo,blog");
		myblog.version = getMap(dbm, "version", "0.29");
		return myblog;
	}
	
	private static Map getMap(Jdoer<Map> dbm, String item, String value) {
		Map map = dbm.unique("item == '" + item + "' && type == '" + Config.TYPE_BLOG + "'");
		if ( map == null ) {
			map = new Map();
			map.setItem(item);
			map.setValue(value);
			map.setType(Config.TYPE_BLOG);
			dbm.save(map);
		}
		return map;
	}
	
	private static String getProperty(String name, String value) {
		String val = Config.getValue(Config.TYPE_BLOG, name);
		if ( val.length() == 0 ) {
			Config conf = new Config();
			conf.setItem(name);
			conf.setType(Config.TYPE_BLOG);
			conf.setValue(value);
			conf.save();
			return value;
		} else {
			return val;
		}
	}
	
	private static int getProperty(String name, int value) {
		try {
			return Integer.parseInt(getProperty(name,""+value));
		} catch (Exception e) {
			return 10;
		}
	}
	
	public Web getWeb() {
		return web;
	}

	public static int getPageSize() {
		return getProperty("pageSize", 10);
	}

	public static int getRecentPosts() {
		return getProperty("recentPosts", 5);
	}

	public static int getRecentComms() {
		return getProperty("recentComments", 5);
	}

	public static int getRssSize() {
		return getProperty("rssSize", 20);
	}

	public static int getAtomSize() {
		return getProperty("atomSize", 20);
	}

	public static int getMapSize() {
		return getProperty("mapSize", 50);
	}

	public String getTitle() {
		return web.getTitle();
	}
	
	public void setTitle(String title) {
		web.setTitle(title);
	}
	
	public String getSubtitle() {
		return web.getSubtitle();
	}
	
	public void setSubtitle(String subtitle) {
		web.setSubtitle(subtitle);
	}
	
	public String getDescription() {
		return web.getDescription();
	}
	
	public void setDescription(String description) {
		web.setDescription(description);
	}
	
	public String getUrl() {
		return web.getUrl();
	}
	
	public void setUrl(String url) {
		if ( ! url.endsWith("/") ) {
			url += "/";
		}
		web.setUrl(url);
	}
	
	public String getTheme() {
		return theme.getValue();
	}
	
	public void setTheme(String value) {
		theme.setValue(value);
	}
	
	public String getKeywords() {
		return keywords.getValue();
	}
	
	public void setKeywords(String value) {
		keywords.setValue(value);
	}
	
	public String getVersion() {
		return version.getValue();
	}
	
	public void setVersion(String value) {
		version.setValue(value);
	}
	
	public String getBase() {
		return base.getValue();
	}

	public void update() {
		UpdateManager.enableStaticTask();
	}

	public static int getTimezone() {
		return getProperty("timezone", 8);
	}
}
